package com.alpha.gateway.service;

import com.alpha.core.exception.AlphaException;
import com.alpha.core.http.Response;
import com.alpha.core.utils.CollectionUtil;
import com.alpha.feign.common.ResourceFeignClient;
import com.alpha.feign.common.TenantFeignClient;
import com.alpha.feign.common.UserFeignClient;
import com.alpha.feign.common.entity.Resource;
import com.alpha.feign.common.entity.Tenant;
import com.alpha.feign.common.entity.User;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

/**
 * common服务feign适配
 *
 * @author chenruwan
 * @since 2022-01-01
 */
@Service
public class FeignClientService {
    private final UserFeignClient userFeignClient;

    private final TenantFeignClient tenantFeignClient;

    private final ResourceFeignClient resourceFeignClient;

    public FeignClientService(@Lazy UserFeignClient userFeignClient,
                              @Lazy TenantFeignClient tenantFeignClient,
                              @Lazy ResourceFeignClient resourceFeignClient) {
        this.userFeignClient = userFeignClient;
        this.tenantFeignClient = tenantFeignClient;
        this.resourceFeignClient = resourceFeignClient;
    }

    public User queryUser(String username, String password) {
        Response<Page<User>> response = supplyAsync(() -> userFeignClient.query(username, password));

        List<User> users = response.getData().getRecords();
        if (CollectionUtil.isEmpty(users)) {
            return null;
        }

        if (users.size() > 1) {
            throw new AlphaException("用户名重复");
        }

        return users.get(0);
    }

    public User getUser(Long userId) {
        Response<User> response = supplyAsync(() -> userFeignClient.get(userId));
        return response.isSuccess() ? response.getData() : null;
    }

    public Tenant getTenant(Long id) {
        Response<Tenant> response = supplyAsync(() -> tenantFeignClient.get(id));
        return response.isSuccess() ? response.getData() : null;
    }

    public List<Resource> getUserResources(String userId) {
        Response<List<Resource>> response = supplyAsync(() -> userFeignClient.getResources(userId));
        return response.isSuccess() ? response.getData() : CollectionUtil.emptyList();
    }

    public List<Resource> getAllResources() {
        Response<Page<Resource>> response = supplyAsync(resourceFeignClient::getResources);
        return response.isSuccess() ? response.getData().getRecords() : CollectionUtil.emptyList();
    }

    private <T> T supplyAsync(Supplier<T> supplier) {
        try {
            return CompletableFuture.supplyAsync(supplier).get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new AlphaException("服务繁忙，请稍后再试", e);
        } catch (ExecutionException e) {
            throw new AlphaException("服务繁忙，请稍后再试", e);
        }
    }
}
